/* 
* hello world
* a sample xmms plugin
*
* iso:crash 01/2002 <iso@kapsobor.de>
*
* latest version at http://iso.kapsobor.de/xmms/
*
*
* all this very general plugin does is displaying a configurable hello-message in the about window
* as well as skipping to the next track every 30 seconds.
* (it's intended for learning purposes only)  
* 
* ******************** */

/* include required header files */

#include <pthread.h>
#include <gtk/gtk.h>
#include "xmmsctrl.h" /* in here you can find all the functions to control the xmms behaviour */
#include <plugin.h>
#include "configfile.h"

/* for the sleep() funtion */
#include <unistd.h>

#define VERSION "0.1.0"

/* the configuration file (xmms handles it very comfortably for us) */
#define CONFIGFILE "/.xmms/helloworld"

/* Gtk stuff */
static GtkWidget *conf_dialog, *about_win;
static pthread_t tid;
static GtkWidget *hellomessage_entry;

/* the funtions */
void *helloworld_thread(void *args);
void helloworld_init(void);
void helloworld_about(void);
void helloworld_config(void);
void helloworld_config_save(GtkWidget *wid, gpointer data);
void helloworld_cleanup(void);

/* the message to be displayed */
gchar *hellomessage = "Hello, World!";

/* the structures for InputPlugins etc can be found in 'plugin.h' */
GeneralPlugin helloworld =
{
	NULL,
	NULL,
	-1,
	"XMMS-Hello World Plugin "VERSION,
	helloworld_init,
	helloworld_about,
	helloworld_config,
	helloworld_cleanup,
};

/* simply stop the running thread if running plugin gets disabled */
void helloworld_cleanup(void)
{
	pthread_cancel (tid);
}

/* required by xmms to display info line */
GeneralPlugin *get_gplugin_info(void)
{
	return (&helloworld);
}

/* read config file ( just one string in it ) */
void read_config (GtkWidget *wid, gpointer data)
{
	gchar *configfile;
	ConfigFile *config;

	configfile = g_strconcat (g_get_home_dir(), CONFIGFILE, NULL);
	if ((config = xmms_cfg_open_file (configfile)) != NULL)
	{
		xmms_cfg_read_string (config, "helloworld", "hellomessage", &hellomessage);
	}
	g_free(configfile);
}

/* initialization of the plugin */
void helloworld_init(void)
{
	pthread_attr_t attr;
	read_config(NULL, NULL);
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
	pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

	pthread_create(&tid, &attr, helloworld_thread, NULL);
	return;
}

/* the main part */
void *helloworld_thread(void *args)
{
	while(1)
	{
		xmms_remote_playlist_next(helloworld.xmms_session);
		sleep(30);
	}
}

/* the about window */
void helloworld_about(void)
{
	GtkWidget *button, *label, *bigbox, *buttonbox;

	if(about_win)
		return;
	about_win = gtk_window_new(GTK_WINDOW_DIALOG);
	gtk_window_set_title(GTK_WINDOW(about_win), ("About"));
	gtk_window_set_policy(GTK_WINDOW(about_win), FALSE, FALSE, FALSE);
	gtk_window_set_position(GTK_WINDOW(about_win), GTK_WIN_POS_MOUSE);

	bigbox = gtk_vbox_new(FALSE, 5);
	gtk_container_add(GTK_CONTAINER(about_win), bigbox);

	label = gtk_label_new(g_strconcat((gchar *)"--------------------------\n", hellomessage, (gchar *)"\n--------------------------\n\nXMMS Helloworld plugin\n\nHas a configurable HelloWorld Message and -as a dummy function- skips to the next Song every 30 seconds.\n\niso:crash <iso@kapsobor.de>", NULL));
	gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
	gtk_container_add(GTK_CONTAINER(bigbox), label);

	buttonbox = gtk_hbutton_box_new();
	gtk_button_box_set_layout(GTK_BUTTON_BOX(buttonbox), GTK_BUTTONBOX_DEFAULT_STYLE);
	gtk_button_box_set_spacing(GTK_BUTTON_BOX(buttonbox), 5);
	gtk_box_pack_start(GTK_BOX(bigbox), buttonbox, FALSE, FALSE, 0);
	gtk_signal_connect(GTK_OBJECT(about_win), "destroy", GTK_SIGNAL_FUNC(gtk_widget_destroyed), &about_win);
	button = gtk_button_new_with_label("Check.");
	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
	gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer) about_win);
	gtk_box_pack_start(GTK_BOX ((buttonbox)), button, FALSE, TRUE, 5);
	gtk_widget_show_all(about_win);
}

/* save config file */
void helloworld_config_save(GtkWidget *wid, gpointer data)
{
	gchar *configfile;
	ConfigFile *config;
	configfile = g_strconcat(g_get_home_dir(), CONFIGFILE, NULL);

	if ((config = xmms_cfg_open_file(configfile)) == NULL)
		config = xmms_cfg_new();

	hellomessage = g_strdup(gtk_entry_get_text(GTK_ENTRY(hellomessage_entry)));
	
	xmms_cfg_write_string (config, "helloworld", "hellomessage", hellomessage);
	xmms_cfg_write_file (config, configfile);
	xmms_cfg_free(config);
	g_free(configfile);

	return;
}

/* the config-ok button */
void helloworld_config_ok(GtkWidget *wid, gpointer data)
{
	helloworld_config_save(NULL, NULL);
	gtk_widget_destroy(conf_dialog);
	conf_dialog = NULL;
	return;
}

/* the config window */
void helloworld_config(void)
{
	GtkWidget 	*ok_button, *apply_button, *cancel_button, *buttonbox,
			*bigbox, *textframe, *hellomessage_label, *ctable;
			
	if (conf_dialog)
		return;
	conf_dialog = gtk_window_new(GTK_WINDOW_DIALOG);

	gtk_window_set_title(GTK_WINDOW(conf_dialog), ("XMMS-Helloworld setup"));
	gtk_window_set_policy(GTK_WINDOW(conf_dialog), FALSE, FALSE, FALSE);
	gtk_window_set_position(GTK_WINDOW(conf_dialog), GTK_WIN_POS_MOUSE);
	gtk_container_set_border_width(GTK_CONTAINER(conf_dialog), 5);
	gtk_signal_connect(GTK_OBJECT(conf_dialog), "destroy", GTK_SIGNAL_FUNC(gtk_widget_destroyed), &conf_dialog);
	
	bigbox = gtk_vbox_new(FALSE, 5);
	gtk_container_add(GTK_CONTAINER (GTK_WINDOW(conf_dialog)), bigbox);
	
	textframe = gtk_frame_new ("Config:");
	gtk_container_add(GTK_CONTAINER(bigbox), textframe);
	ctable = gtk_table_new(2, 1, FALSE);
	gtk_container_add(GTK_CONTAINER(textframe), ctable);
	hellomessage_label = gtk_label_new("Hello Message:");
	gtk_table_attach_defaults(GTK_TABLE(ctable), hellomessage_label, 0, 1, 0, 1 );
	hellomessage_entry = gtk_entry_new();
	if(hellomessage != NULL)
		gtk_entry_set_text(GTK_ENTRY(hellomessage_entry), hellomessage);
	gtk_table_attach_defaults(GTK_TABLE(ctable), hellomessage_entry, 1, 2, 0, 1);
	
	buttonbox = gtk_hbutton_box_new();
	gtk_button_box_set_layout(GTK_BUTTON_BOX(buttonbox), GTK_BUTTONBOX_END);
	gtk_button_box_set_spacing(GTK_BUTTON_BOX(buttonbox), 5);
	gtk_box_pack_start(GTK_BOX(bigbox), buttonbox, FALSE, FALSE, 0);
	ok_button = gtk_button_new_with_label("Ok");
	apply_button = gtk_button_new_with_label("Apply");
	cancel_button = gtk_button_new_with_label("Cancel");

	gtk_signal_connect_object(GTK_OBJECT(cancel_button), "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) conf_dialog);
	gtk_signal_connect_object(GTK_OBJECT(apply_button), "clicked", GTK_SIGNAL_FUNC (helloworld_config_save), NULL);
	gtk_signal_connect_object(GTK_OBJECT(ok_button), "clicked", GTK_SIGNAL_FUNC (helloworld_config_ok), NULL);
	
	GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT);
	GTK_WIDGET_SET_FLAGS(cancel_button, GTK_CAN_DEFAULT);
	GTK_WIDGET_SET_FLAGS(apply_button, GTK_CAN_DEFAULT);

	gtk_box_pack_start(GTK_BOX(buttonbox), ok_button, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(buttonbox), apply_button, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(buttonbox), cancel_button, TRUE, TRUE, 0);

	gtk_widget_show_all(conf_dialog);
}

/* end of file */
